<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Log;

class Workcorp extends Model
{
    protected $table = 'work_corp';
    protected $primaryKey = 'id';
    
    public static function userWorkCropList($users_id,$name)
    {
        $query = self::where("users_id",$users_id)->where("disable","!=",4);
        if (!empty($name)) $query->where("name","like","%".$name."%");
        return $query->get();
    }

    public static function userWorkCorpSample($users_id)
    {
        return self::select("corp_id","name")->where("users_id",$users_id)->orderBy("id","DESC")->get();
    }

    public static function userWorkCorpCount($users_id)
    {
        return self::where("users_id",$users_id)->where("disable",2)->count("id");
    }

    public static function getWorkCorpDetial($corp_id)
    {
        return self::where("corp_id",$corp_id)->first();
    }


    public static function getKfCorpsPermanentCode($corp_id)
    {
        return self::select("permanent_code","suite_id")->where("corp_id",$corp_id)->orderBy("id","DESC")->first();
    }

    public static function getWorkCorpDetialId($corp_id)
    {
        return self::select("id","suite_id")->where("corp_id",$corp_id)->orderBy("id","DESC")->first();
    }

    public static function workCorpCancel($corp_id,$suite_id)
    {
        return self::where("corp_id",$corp_id)->where("suite_id",$suite_id)->update(["disable"=>3]);
    }

    public static function workCorpDelete($corp_id,$suite_id)
    {
        return self::where("corp_id",$corp_id)->where("suite_id",$suite_id)->update(["disable"=>4]);
    }

    public static function workCorpCreate($users_id,$corp_id,$corp_name,$square_logo_url,$permanent_code,$suite_id,$agent_id,$verified_end_time)
    {
        return self::insertGetId(["users_id"=>$users_id,"corp_id"=>$corp_id,"name"=>$corp_name,"square_logo_url"=>$square_logo_url,"permanent_code"=>$permanent_code,"suite_id"=>$suite_id,"agent_id"=>$agent_id,"verified_end_time"=>$verified_end_time,"disable"=>2,"created_at"=>time()]);
    }

    public static function workCorpRebind($corp_id,$permanent_code,$name,$square_logo_url,$agent_id,$verified_end_time)
    {
        return self::where("corp_id",$corp_id)->update(["name"=>$name,"square_logo_url"=>$square_logo_url,"permanent_code"=>$permanent_code,"agent_id"=>$agent_id,"verified_end_time"=>$verified_end_time,"disable"=>2]);
    }
}